package fussbudget.gui;

import javax.swing.JTabbedPane;

import org.joda.time.DateMidnight;

import com.miginfocom.calendar.activity.Activity;
import com.miginfocom.calendar.activity.ActivityDepository;
import com.miginfocom.calendar.activity.DefaultActivity;
import com.miginfocom.calendar.category.Category;
import com.miginfocom.calendar.category.CategoryDepository;
import com.miginfocom.calendar.category.CategoryStructureEvent;
import com.miginfocom.util.dates.DateRange;
import com.miginfocom.util.dates.ImmutableDateRange;

import fussbudget.PaymentEntity;
import fussbudget.ScheduledTransaction;

public class CalendarMaster {
	public static Category		root			= CategoryDepository.getRoot();
	public static CalendarPanel	calendarPanel	= null;
	Integer						incomeId;
	Integer						expensesId;
	private static Category			incomeCategory;

	public CalendarMaster() {
		super();
		incomeId = 1;
		expensesId = 2;

		incomeCategory = new Category(incomeId, "Income", root.getId());
		Category expenseCategory = new Category(expensesId, "Expenses", root.getId());
		CategoryDepository.addBrokedCategory(incomeCategory, root, CategoryStructureEvent.ADDED_CREATED);
		CategoryDepository.addBrokedCategory(expenseCategory, root, CategoryStructureEvent.ADDED_CREATED);

		addScheduledTransaction(null);

		refreshCalendar();
	}

	public static void addScheduledTransaction(ScheduledTransaction transaction) {
		ImmutableDateRange actRange = new ImmutableDateRange(new DateMidnight().getMillis(), DateRange.RANGE_TYPE_DAY, 1, null, null);
		Activity activity = new DefaultActivity(actRange, new Integer(1234));
		activity.setSummary("Hello, World!");
		activity.setCategoryIDs(new Object[]{incomeCategory.getId()});

		ActivityDepository.getInstance().addBrokedActivity(activity, null);
	}

	public static void refreshCalendar() {
		JTabbedPane p = null;
		if (calendarPanel != null) {
			p = (JTabbedPane) calendarPanel.getParent();
			if (p != null) {
				p.remove(calendarPanel);
			}
		}
		calendarPanel = new CalendarPanel(root);
		if (p != null) {
			p.insertTab("Calendar", null, calendarPanel, "huh", 0);
		}

	}

	public static void setParentGroup(PaymentEntity child, PaymentEntity parent) {
		if (CategoryDepository.containsCategory(child.getId()) && CategoryDepository.containsCategory(parent.getId())) {
			removeGroup(child);
			addGroup(child.getId(), child.getName(), parent.getId());
		}

	}

	public static void addIncomeGroup(PaymentEntity newGroup) {
		addGroup(newGroup.getId(), newGroup.getName(), 1);

	}

	public static void addExpensesGroup(PaymentEntity newGroup) {
		addGroup(newGroup.getId(), newGroup.getName(), 2);

	}

	public static void addGroup(int id, String name, int parentCategoryID) {
		Category newCategory = new Category(id, name, parentCategoryID);
		CategoryDepository.addBrokedCategory(newCategory, root, CategoryStructureEvent.ADDED_CREATED);
		refreshCalendar();

	}

	public static void removeGroup(PaymentEntity paymentEntity) {
		if (CategoryDepository.containsCategory(paymentEntity.getId())) {
			CategoryDepository.removeBrokedCategoryById(paymentEntity.getId(), root, CategoryStructureEvent.REMOVED_DELETED);
		}

	}

	public static void renameGroup(int id, String newName) {
		if (CategoryDepository.containsCategory(id)) {
			Category currCategory = CategoryDepository.getCategory(id);
			currCategory.setName(newName);
		}

	}
}
